package com.sunducation.wts.databinding4

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunducation.wts.databinding4.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var ownerManualModel:OwnerManualModel
    private var mAdapter : MyAdapter<OwnerManualBean>? =null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        var binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)


        ownerManualModel = ViewModelProviders.of(this).get(OwnerManualModel::class.java)
        ownerManualModel.getManualsData()
        val layoutManager = LinearLayoutManager(this)
        binding.recyclerManual.layoutManager = layoutManager
        mAdapter = MyAdapter(R.layout.item_layout)
        binding.recyclerManual.adapter = mAdapter
        mAdapter?.setOnItemClickListener(object : BaseAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                Toast.makeText(this@MainActivity, "点击了${position}", Toast.LENGTH_LONG).show()

            }
        })


        ownerManualModel.listManuals.observe(this,
            Observer {
                //此处更新UI，将数据赋值并更新adapter
                mAdapter!!.refreshData(it)
                mAdapter?.notifyDataSetChanged()
            })
    }
}
